package com.seckill.mapper;


import com.seckill.pojo.entity.SeckillActivity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 秒杀活动Mapper接口
 */
@Mapper
public interface SeckillActivityMapper {

    /**
     * 新增秒杀活动
     *
     * @param seckillActivity
     * @return
     */
    @Insert("INSERT INTO seckill_activity (name, start_time, end_time, status, description, image, stock, sold_count, create_time, update_time, create_user, update_user) " +
            "VALUES (#{name}, #{startTime}, #{endTime}, #{status}, #{description}, #{image}, #{stock}, #{soldCount}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(SeckillActivity seckillActivity);

    /**
     * 分页查询秒杀活动
     * @param name
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM seckill_activity " +
            "<where>" +
            "<if test='name != null and name != \"\"'>" +
            "name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<SeckillActivity> pageQuery(@Param("name") String name);

    /**
     * 根据id查询秒杀活动
     * @param id
     * @return
     */
    @Select("SELECT * FROM seckill_activity WHERE id = #{id}")
    SeckillActivity getById(Long id);

    /**
     * 修改秒杀活动
     *
     * @param seckillActivity
     * @return
     */
    @Update("UPDATE seckill_activity SET name = #{name}, start_time = #{startTime}, end_time = #{endTime}, " +
            "status = #{status}, description = #{description}, image = #{image}, stock = #{stock}, " +
            "sold_count = #{soldCount}, update_time = #{updateTime}, update_user = #{updateUser} " +
            "WHERE id = #{id}")
    Integer update(SeckillActivity seckillActivity);

    /**
     * 删除秒杀活动
     *
     * @param id
     * @return
     */
    @Delete("DELETE FROM seckill_activity WHERE id = #{id}")
    Integer delete(Long id);

    /**
     * 更新活动状态
     * @param id
     * @param status
     */
    @Update("UPDATE seckill_activity SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    void updateStatus(@Param("id") Long id, @Param("status") Integer status);

    /**
     * 查询进行中的活动
     * @return
     */
    @Select("SELECT * FROM seckill_activity WHERE status = 1 ORDER BY create_time DESC")
    List<SeckillActivity> queryOngoingActivities();
}